<template>
  <div>
    <search-table
      :url="url"
      ref="searchtable"
      :columns="columns"
      :form="form"
      :defaultParams="defaultParams"
    >
      <template slot="searchForm">
        <a-form layout="inline" :form="form">
          <!-- <a-form-item label="状态">
                <a-select style="width: 180px"
                v-decorator="[
                    'goodsStatus',
                ]" :allowClear="true" placeholder="请选择" :options="allStatus"></a-select>
            </a-form-item> -->
          <a-form-item label="商品名称">
            <a-input
              style="width: 180px"
              v-decorator="['goodsName']"
              placeholder="请输入商品名称"
            >
            </a-input>
          </a-form-item>
        </a-form>
      </template>
      <template slot="oprationRow">
        <permission>
          <a-button type="primary" icon="plus" @click="chooseGoods()"
            >选择商品</a-button
          >
        </permission>
      </template>
      <template slot="tableOperation" slot-scope="{ record }">
        <a-space>
          <permission authority="discount:goodsnorms:list">
            <a
              href="javascript:;"
              class="text-primary"
              @click="handleDetail(record)"
              >查看详情</a
            >
          </permission>
          <permission
            authority="discount:goodsnorms:modify"
            v-if="activityStatu != 3"
          >
            <a
              href="javascript:;"
              class="text-primary"
              @click="handleEditCommodity(record)"
              >编辑商品</a
            >
          </permission>
          <permission
            authority="discount:goods:delete"
            v-if="activityStatu != 3"
          >
            <a
              href="javascript:;"
              class="text-danger"
              @click="deleteRows(record.goodId)"
              >删除</a
            >
          </permission>
        </a-space>
      </template>
    </search-table>
    <ChooseGoods
      :visible="chooseGoodsVisible"
      @cancel="chooseGoodsVisible = false"
      @submit="handleChooseGoodsSubmit"
    ></ChooseGoods>
  </div>
</template>

<script>
import ChooseGoods from "./components/ChooseGoods";
import urlParse from "@/utils/urlParse";

export default {
  components: {
    ChooseGoods,
  },
  computed: {
    activityId() {
      return urlParse("activityId") || "";
    },
    activityStatu() {
      return this.$route.query.activityStatu || "";
    },
  },
  data() {
    return {
      url: "goldenhome/seckill/admin_/discount/goods/page",
      defaultParams: {
        activityId: urlParse("activityId"),
      },
      form: this.$form.createForm(this, { name: "table_search" }),
      columns: [
        {
          title: "商品名称",
          dataIndex: "goodsName",
          key: "goodsName",
        },
        {
          title: "现有库存",
          dataIndex: "totalDiscountStock",
          key: "totalDiscountStock",
        },
        {
          title: "折扣剩余库存",
          dataIndex: "totalRemainStock",
          key: "totalRemainStock",
        },
        {
          title: "折扣总库存",
          dataIndex: "goodsInventoryTotal",
          key: "goodsInventoryTotal",
        },
        {
          title: "商品状态",
          dataIndex: "goodsStatus",
          key: "goodsStatus",
          customRender: (text) => {
            if (text == 1) {
              return "仓库中";
            } else if (text == 2) {
              return "上架";
            } else if (text == 3) {
              return "已下架";
            }
          },
        },
        {
          title: "操作",
          dataIndex: "operation",
          scopedSlots: { customRender: "operation" },
        },
      ],
      allStatus: [
        {
          value: "",
          label: "全部状态",
        },
        {
          value: 1,
          label: "未开始",
        },
        {
          value: 2,
          label: "进行中",
        },
        {
          value: 3,
          label: "已结束",
        },
      ],
      chooseGoodsVisible: false,
      currentData: {},
    };
  },
  created() {
    //alert(this.activityId);
  },
  methods: {
    handleCancel() {
      this.newActivityVisible = false;
      this.currentData = {};
    },
    //选择商品
    handleChooseGoodsSubmit(id) {
      this.newActivityVisible = false;
      this.$router.push({
        name: "MarketingDiscountAddEditCommodity",
        query: {
          activityId: this.activityId,
          productId: id,
          type: 1,
        },
      });
    },
    //编辑商品
    handleEditCommodity(record) {
      this.$router.push({
        name: "MarketingDiscountAddEditCommodity",
        query: {
          activityId: this.activityId,
          productId: record.goodId,
          type: 2,
        },
      });
    },
    handleDetail(record) {
      this.$router.push({
        name: "MarketingDiscountDetailCommodity",
        query: {
          activityId: this.activityId,
          productId: record.goodId,
        },
      });
    },
    chooseGoods() {
      this.chooseGoodsVisible = true;
    },
    deleteRows(id) {
      this.$confirm({
        title: "删除提示",
        content:
          "确定删除次商品，删除后将不可恢复，若已开启活动则该商品将被强制下架",
        okText: "确认删除",
        cancelText: "取消",
        onOk: () => {
          //console.log(ids);
          this.$post("goldenhome/seckill/admin_/discount/goods/delete", [
            {
              activityId: this.activityId,
              goodsId: id,
            },
          ]).then((res) => {
            if (res && res.returnCode == "1000") {
              this.$message.success("删除成功");
              this.$refs.searchtable.refreshTable();
            }
          });
        },
      });
    },
  },
};
</script>

<style lang="less" scoped>
.disabled {
  pointer-events: none;
  color: #ccc;
}
/deep/ .search-form {
  display: flex;
  // .seach-btn-list{
  //     margin-top:-16px;
  // }
  .ant-btn {
    margin-top: 0 !important;
  }
}
</style>